package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.SaleAreaAdapter;
import com.luck.easyfoodbatch.db.FindDB;
import com.luck.easyfoodbatch.entity.City;
import com.luck.easyfoodbatch.entity.CityEntity;
import com.luck.easyfoodbatch.indexablelistview.IndexEntity;
import com.luck.easyfoodbatch.indexablelistview.IndexHeaderEntity;
import com.luck.easyfoodbatch.indexablelistview.IndexableStickyListView;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SaleAreaActivity extends BaseActivity {

    @BindView(R.id.left_back)
    ImageButton leftBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.input_search_query)
    EditText inputSearchQuery;
    @BindView(R.id.searchBarContainer)
    RelativeLayout searchBarContainer;
    @BindView(R.id.indexListView)
    IndexableStickyListView indexListView;
    private final String EXTRA_LARGE = "extra_large";
    @BindView(R.id.ll_del)
    LinearLayout llDel;
    private SaleAreaAdapter mAdapter;
    private List<CityEntity> mCities = new ArrayList<>();
    private FindDB findDB;
    private int position = -1, index = -1;
    private String tempId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location, "售卖地区", "确定", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        findDB = new FindDB(this);
        index = getIntent().getIntExtra("index", -1);
        position = getIntent().getIntExtra("position", -1);
        tempId = getIntent().getStringExtra("tempId");// 选中的地区id
        initView();
        inputSearchQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0) {
                    llDel.setVisibility(View.VISIBLE);
                } else {
                    llDel.setVisibility(View.GONE);
                }
            }
        });
    }

    @OnClick({R.id.ll_del})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_del:
                inputSearchQuery.setText("");
                break;
        }
    }

    @Override
    public void rightClick() {
        super.rightClick();
        int i = 0;
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        for (CityEntity item : mCities) {
            if (item.isChecked()) {
                if (i == 0) {
                    // 第一次的时候
                    sb1.append("#").append(item.getId()).append("#");
                } else {
                    sb1.append(item.getId()).append("#");
                }
                sb2.append(item.getName()).append(",");
                i++;
            }
        }
        String ids = sb1.toString();
        String names = Tools.tempNames(sb2.toString());
        Intent intent = new Intent();
        intent.putExtra("id", ids);
        intent.putExtra("name", names);
        intent.putExtra("position", position);
        intent.putExtra("index", index);
        setResult(RESULT_OK, intent);
        finish();
    }

    protected void initView() {
        mAdapter = new SaleAreaAdapter(this);
        indexListView.setAdapter(mAdapter);

        // 初始化数据
        List<City> provinces = findDB.getProvinces();
        for (City item : provinces) {
            CityEntity cityEntity = new CityEntity();
            cityEntity.setName(item.getName());
            cityEntity.setId(item.getId());
            if (!Tools.isNull(tempId)) {
                // 如果有选中的数据
                String[] split = tempId.split("#");
                for (int i = 0; i < split.length; i++) {
                    String id = split[i];
                    if (cityEntity.getId().equals(id)) {
                        cityEntity.setChecked(true);
                    }
                }
            } else {
                cityEntity.setChecked(false);
            }
            mCities.add(cityEntity);
        }

        if (getIntent().getBooleanExtra(EXTRA_LARGE, false)) {
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
        }

        // 绑定数据
        indexListView.bindDatas(mCities);

        // 搜索
        inputSearchQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                indexListView.searchTextChange(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        indexListView.setOnItemContentClickListener(new IndexableStickyListView.OnItemContentClickListener() {
            @Override
            public void onItemClick(View v, IndexEntity indexEntity) {
                if (indexEntity.isChecked()) {
                    indexEntity.setChecked(false);
                } else {
                    indexEntity.setChecked(true);
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }
}
